<?php
/**
 * MottoController.class.php
 * 幸福语录控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-04-27
 */

namespace Admin\Controller;

class MottoController extends BaseController {

	/**
	 * 幸福语录列表获取
	 * @param 	$page
	 * @return 	json 	$list
	 */
	function getList(){
		$condition 	= $this->request('condition', []);
		$page 	= $this->request('p', 1);
		$pageHtml 	= true;

		$this->response([
			'list'	=> D('Motto')->getList($condition, $page, $pageHtml),
			'page_html'	=> $pageHtml
		]);
		// $page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		// $pageHtml 	= true;

		// $list 	= D('Motto')->getList($page, $pageHtml);
		// if($list === false){
		// 	$this->outOInfo(D('Motto')->errMsg, 0);
		// }
		// $this->outOInfo(['list' => $list, 'page_html' => $pageHtml]);
	}

	/**
	 * 添加幸福语录
	 * @param 	$title
	 * @param 	$picture
	 * @param 	$audio
	 * @return 	boolean
	 */
	function addItem(){
		$this->unEmptyParam(['title', 'picture', 'audio']);
		$mottoInfo 	= [];

		//客户端数据抓取
		$title 	= $this->request('title');
		$picture 	= $this->request('picture');
		$audio 	= $this->request('audio');
		$onlineDate 	= $this->request('online_date');

		//参数整理
		$picture 	= strchr($picture, '/server');
		$audio 	= strchr($audio, '/server');
		$audioTime 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');
		$onlineTime 	= $onlineDate ? strtotime($onlineDate) : time();
		$status 	= $onlineDate ? 2 : 1;

		D('Motto')->add([
			'title'	=> $title,
			'picture'	=> $picture,
			'audio'	=> $audio,
			'audio_time'	=> $audioTime,
			'create_time'	=> time(),
			'online_time'	=> $onlineTime,
			'status'	=> $status
		]);

		$this->response();
	}

	/**
	 * 编辑幸福语录
	 * @param 	$motto_id
	 * @param 	$motto_info
	 * @return 	boolean
	 */
	function updateItem(){
		$this->unEmptyParam(['motto_id', 'motto_info']);

		$mottoId 	= $this->request('motto_id');
		$mottoInfo 	= $this->request('motto_info');

		//字段转义
		if($mottoInfo['audio']){
			$mottoInfo['audio'] 	= strchr($mottoInfo['audio'], '/server');
			$mottoInfo['audio_time'] 	= get_audio_info(strchr($mottoInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		if($mottoInfo['picture']){
			$mottoInfo['picture'] 	= strchr($mottoInfo['picture'], '/server');
		}
		// if($mottoInfo['online_date']){
		// 	$mottoInfo['online_time']	= strtotime($mottoInfo['online_date']);
		// }

		D('Motto')->where('id = %d', $mottoId)->save($mottoInfo);

		$this->response();
	}
	
}